package com.epalmpay.service.dataanalyze.impl;

import com.epalmpay.dto.analyze.CardAnalyzeExportOutDTO;
import com.epalmpay.dto.analyze.CardAnalyzeInDTO;
import com.epalmpay.dto.analyze.CardAnalyzeOutDTO;
import com.epalmpay.dto.analyze.CardDataOutDTO;
import com.epalmpay.entity.Industry;
import com.epalmpay.mapper.IndustryMapper;
import com.epalmpay.mapper.TradeOfflineMapper;
import com.epalmpay.service.dataanalyze.ICardDataAnalyzeService;
import com.epalmpay.util.AmountUtils;
import com.epalmpay.util.DateUtil;
import com.epalmpay.util.date.TwoDateDays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CardDataAnalyzeServiceImpl implements ICardDataAnalyzeService {
    @Autowired
    private TradeOfflineMapper tradeOfflineMapper;
    @Autowired
    private IndustryMapper industryMapper;
    @Override
    public CardAnalyzeOutDTO queryCardTongji(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception{
        //

        //2代表就是查前七天  不然根据日期查一个月
        System.out.println(cardAnalyzeInDTO);
        if(2==cardAnalyzeInDTO.getIsQueryDayDate()){
            String yyymmdd = TwoDateDays.BeforeDay(7);  //得到七天前的日期
            cardAnalyzeInDTO.setTianyyyyMMdd(yyymmdd); //结束时间
            cardAnalyzeInDTO.setStartYyyyMmdd(DateUtil.fomatDate(new Date(),DateUtil.sdfDay)); //开始时间
            System.out.println(cardAnalyzeInDTO);
            return  tradeOfflineMapper.BeforeSevenDaysCardData(cardAnalyzeInDTO);
        }else{
            return  tradeOfflineMapper.queryCardTongji(cardAnalyzeInDTO);
        }

    }

    /**
     * 上月同期的一个星期的数据
     * @param cardAnalyzeInDTO
     * @return
     */
    @Override
    public CardAnalyzeOutDTO preBeforeSevenDaysCardData(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception{
        String startYyyyMmdd = TwoDateDays.BeforeDay(7);  //得到七天前的日期
        cardAnalyzeInDTO.setStartYyyyMmdd(startYyyyMmdd);

        String tianyyyyMMdd = TwoDateDays.BeforeDay(14);  //得到14天前的日期
        cardAnalyzeInDTO.setTianyyyyMMdd(tianyyyyMMdd);
        return tradeOfflineMapper.BeforeSevenDaysCardData(cardAnalyzeInDTO);
    }

    @Override
    public CardDataOutDTO cardAnalyzeTu(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception {
        if(2==cardAnalyzeInDTO.getIsQueryDayDate()){ //最近一周    String yyymmdd = TwoDateDays.BeforeDay(7);  //得到七天前的日期
            String yyymmdd = TwoDateDays.BeforeDay(7);  //得到七天前的日期

            cardAnalyzeInDTO.setTianyyyyMMdd(yyymmdd); //结束时间
            cardAnalyzeInDTO.setStartYyyyMmdd(DateUtil.fomatDate(new Date(),DateUtil.sdfDay)); //开始时间
        }
        List<CardAnalyzeOutDTO> cdList = tradeOfflineMapper.cardAnalyzeTu(cardAnalyzeInDTO);   //本月或最近一周的卡券核销总数分组

        List<CardAnalyzeOutDTO> cdList2 = tradeOfflineMapper.cardAnalyzeTu2(cardAnalyzeInDTO); //本月或最近一周的大商户发优惠卷的总数分组
        CardDataOutDTO cardDataOutDTO=new CardDataOutDTO();
        int[] month_days = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        int year = Integer.parseInt(cardAnalyzeInDTO.getDateAnalye().split("-")[0]);
        int month = Integer.parseInt(cardAnalyzeInDTO.getDateAnalye().split("-")[1]);
        int days = 0;
        if(month == 2 && (year%4==0&&year%100!=0||year%400==0)) {//闰2月
            days = 29;
        } else {
            days = month_days[month];
        }
        //构造一周的数据(根据天数) 7天
        List list = null;
        if(2==cardAnalyzeInDTO.getIsQueryDayDate()){
            list = TwoDateDays.TwoDateBetweenDays(cardAnalyzeInDTO.getTianyyyyMMdd(), cardAnalyzeInDTO.getStartYyyyMmdd());
        }else{     //构造30天的数据(根据天数) 30天
            String firstMonth = TwoDateDays.getFirstMonth(cardAnalyzeInDTO.getDateAnalye(), "yyyy-MM", "yyyy-MM-dd");//获取某月的第一天
            String lastMonth = TwoDateDays.getLastMonth(cardAnalyzeInDTO.getDateAnalye(), "yyyy-MM", "yyyy-MM-dd");//获取某月的最后一天
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String dateAnalye = cardAnalyzeInDTO.getDateAnalye();
            long time = sdf.parse(dateAnalye).getTime();
            long time2 = new Date().getTime();
            if(cardAnalyzeInDTO.getDateAnalye().equals(DateUtil.fomatDate(new Date(), DateUtil.sdfMonth))){ //如果前台选择传入过来的日期等于当前日期  数据就只构造到当前日期的几号
                list =TwoDateDays.TwoDateBetweenDays(firstMonth,DateUtil.fomatDate(new Date(), DateUtil.sdfDay));
            }else if(time>time2){
                list = new ArrayList();
            }else{
                list =TwoDateDays.TwoDateBetweenDays(firstMonth,lastMonth);
            }

           /* for(int i=1;i<=days;i++){
                Map map = new HashMap();
                // cardDataOutDTO.getMonthDays().addFirst(year + "-" + (month<10?"0"+month:month) + "-"+(days<10?"0"+i:i));
                map.put("createTime",year + "-" + (month<10?"0"+month:month) + "-"+(i<10?"0"+i:i));
                map.put("cardKxTotal", 0);
                map.put("preCardKxTotal",0);  // 上个月(最近一周的上周)的卡券核销总数
                map.put("kxMoneyTotal",0);  //核销金额总计
                map.put("issueTotal", 0);
                map.put("preissueTotal", 0);  //上个月(最近一周的上周)的发放张数
                map.put("cardKxtqContrast",0); //初始化卡券核销同期对比
                map.put("issuetqContrast",0); //初始化大商户发优惠卷的同期对比
                map.put("prekxMoneyTotal",0);  //上期的核销金额总计
                map.put("kxMoneyTotalContrast",0);
                map.put("preissueMembership",0);  //上期发放会员人数
                map.put("issueMembership",0);  //本期发放会员人数
                map.put("issueMembershipContrast","+"+0);
                map.put("issueMoneyTotal",0);  //本期发放金额总计
                map.put("preissueMoneyTotal",0);  //上期发放金额总计
                map.put("issueMoneyTotalContrast","+"+0);
                map.put("issueBiShu",0);            //本期发放笔数
                map.put("preissueBiShu",0);          //上期发放笔数
                map.put("issueBiShuContrast","+"+0);
                map.put("issueAvgPrice",0);           //本期发放平均客单价
                map.put("preissueAvgPrice",0);        //上期发放平均客单价
                map.put("issueAvgPriceContrast","+"+0);
                map.put("kxAvgPrice",0);             //本期核销平均客单价
                map.put("prekxAvgPrice",0);            //上期核销平均客单价
                map.put("kxAvgPriceContrast","+"+0);
                map.put("issueRenContrast","+"+0);
                map.put("kxTotalContrast2","+"+0);
                list.add(map);
            }*/
        }
        if(list.size()>0||null!=list){

        System.out.println(list);
        CardAnalyzeInDTO cdin=new CardAnalyzeInDTO();
        int preYear;
        int preMonth;
        if(month==1||"1".equals(month)){   //如果是一月份    2018-01
            preYear=year-1;  //年份减一年
            preMonth=12;    //月份变为12月份
        }else{     // 否则只减月份
            preYear=year;
            preMonth=month-1;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        cdin.setGroupId(cardAnalyzeInDTO.getGroupId());
        cdin.setDateAnalye(preYear+"-"+(preMonth<10?"0"+preMonth:preMonth));//上个月的日期
        if(2==cardAnalyzeInDTO.getIsQueryDayDate()){ //最近一周的上一周的日期
            String startYyyyMmdd = TwoDateDays.BeforeDay(7);  //得到七天前的日期
            cdin.setStartYyyyMmdd(startYyyyMmdd); //最大的日期

            String tianyyyyMMdd = TwoDateDays.BeforeDay(14);  //得到14天前的日期
            cdin.setTianyyyyMMdd(tianyyyyMMdd);  //最小的日期
            cdin.setIsQueryDayDate(2);
        }
        List<CardAnalyzeOutDTO> precdList =tradeOfflineMapper.cardAnalyzeTu(cdin); //上月的或最近一周的卡券核销总数分组
        List<CardAnalyzeOutDTO> precdList2 =tradeOfflineMapper.cardAnalyzeTu2(cdin); //上月或最近上一周的大商户发优惠卷的总数分组

         //上个月有数据 就把数据放入Map 集合中 没有 就是默认值
        if(null!=precdList||precdList.size()>0){
            for(CardAnalyzeOutDTO currentMonth:precdList){  //上个月或上周
                String datet2 =dateFormat.format(currentMonth.getCreateData());  //查询出来的日期
                for (int i=0;i<list.size();i++){
                    Map m=(HashMap)list.get(i);
                    String preDay = datet2.substring(8, 10);
                    String curDay = m.get("createTime").toString().substring(8,10);
                    if(2==cardAnalyzeInDTO.getIsQueryDayDate()) {   //上周 与 上上周的每一天做对比
                        String date= m.get("createTime").toString();     //2018-03-15
                        Date d = DateUtil.fomatDate(date);
                        int day=(int)(d.getTime()-currentMonth.getCreateData().getTime())/(24*60*60*1000);
                        if(day==7){
                            m.put("preCardKxTotal",Integer.parseInt(m.get("preCardKxTotal").toString())+currentMonth.getCardKxTotal());
                            m.put("prekxMoneyTotal", Integer.parseInt(m.get("prekxMoneyTotal").toString()) + currentMonth.getKxMoneyTotal()/100); //上期的核销金额总计

                            float prekxAvgPrice=(float)Math.round((currentMonth.getKxMoneyTotal()/100)/currentMonth.getCardKxTotal()*100)/100;   //上期核销平均客单价
                            m.put("prekxAvgPrice", Float.parseFloat(m.get("prekxAvgPrice").toString()) + prekxAvgPrice);

                        }
                    }
                   else if(1==cardAnalyzeInDTO.getIsQueryDayDate()&&preDay.equals(curDay)) {  //已经存在该日期
                        m.put("preCardKxTotal",Integer.parseInt(m.get("preCardKxTotal").toString())+currentMonth.getCardKxTotal());
                        m.put("prekxMoneyTotal", Integer.parseInt(m.get("prekxMoneyTotal").toString()) + currentMonth.getKxMoneyTotal()/100); //上期的核销金额总计

                        float prekxAvgPrice=(float)Math.round((currentMonth.getKxMoneyTotal()/100)/currentMonth.getCardKxTotal()*100)/100;   //上期核销平均客单价
                        m.put("prekxAvgPrice", Float.parseFloat(m.get("prekxAvgPrice").toString()) + prekxAvgPrice);

                    }
                }
            }
        }
          //本月或最近一周的卡券核销总数分组
        for(CardAnalyzeOutDTO clist:cdList){
            String datet =dateFormat.format(clist.getCreateData());
            for (int i=0;i<list.size();i++){
                Map m =(HashMap) list.get(i);
                if(m.containsValue(datet)) {  //已经存在该日期
                   // m.get("cardKxTotal");
                    m.put("cardKxTotal", Integer.parseInt(m.get("cardKxTotal").toString()) + clist.getCardKxTotal()); //这个月的核销总数
                    m.put("kxMoneyTotal", Integer.parseInt(m.get("kxMoneyTotal").toString()) + clist.getKxMoneyTotal()/100); //上期的核销金额总计

                  float kxAvgPrice=(float)Math.round((clist.getKxMoneyTotal()/100)/clist.getCardKxTotal()*100)/100;
                    m.put("kxAvgPrice", Float.parseFloat(m.get("kxAvgPrice").toString()) + kxAvgPrice);
                }
          }
        }

       // 上月的或(上上周的) 大商户发优惠卷的总数分组
        if(null!=precdList2||precdList2.size()>0){
            for(CardAnalyzeOutDTO preMonthList:precdList2){  //上个月或(上上周的)  2018-03-22 --->2018-03-15
                String datet2 =dateFormat.format(preMonthList.getCreateData());  //查询出来的日期
                for (int i=0;i<list.size();i++){                 //2018-03-15 --->2018-03-22
                    Map m=(HashMap)list.get(i);
                    String preDay = datet2.substring(8, 10);
                    String curDay = m.get("createTime").toString().substring(8,10);
                    if(2==cardAnalyzeInDTO.getIsQueryDayDate()){   //上周 与 上上周的每一天做对比
                       String date= m.get("createTime").toString();     //2018-03-15
                        Date d = DateUtil.fomatDate(date);
                        int day=(int)(d.getTime()-preMonthList.getCreateData().getTime())/(24*60*60*1000);
                         if(day==7){
                             m.put("preissueTotal",Integer.parseInt(m.get("preissueTotal").toString())+preMonthList.getIssueTotal());
                             m.put("preissueMembership", Integer.parseInt(m.get("preissueMembership").toString()) + preMonthList.getIssueMembership());
                             m.put("preissueMoneyTotal", Integer.parseInt(m.get("preissueMoneyTotal").toString()) + preMonthList.getIssueMoneyTotal()/100);
                             m.put("preissueBiShu", Integer.parseInt(m.get("preissueBiShu").toString()) + preMonthList.getIssueBiShu());

                             float preissueAvgPrice=(float)Math.round((preMonthList.getIssueMoneyTotal()/100)/preMonthList.getIssueTotal()*100)/100; //上期的发放平均客单价
                             m.put("preissueAvgPrice", Float.parseFloat(m.get("preissueAvgPrice").toString()) + preissueAvgPrice);

                         }
                    }
                    else if(1==cardAnalyzeInDTO.getIsQueryDayDate()&&preDay.equals(curDay)) {  //已经存在该日期
                        m.put("preissueTotal",Integer.parseInt(m.get("preissueTotal").toString())+preMonthList.getIssueTotal());
                        m.put("preissueMembership", Integer.parseInt(m.get("preissueMembership").toString()) + preMonthList.getIssueMembership());
                        m.put("preissueMoneyTotal", Integer.parseInt(m.get("preissueMoneyTotal").toString()) + preMonthList.getIssueMoneyTotal()/100);
                        m.put("preissueBiShu", Integer.parseInt(m.get("preissueBiShu").toString()) + preMonthList.getIssueBiShu());
                        float preissueAvgPrice=(float)Math.round((preMonthList.getIssueMoneyTotal()/100)/preMonthList.getIssueTotal()*100)/100; //上期的发放平均客单价
                        m.put("preissueAvgPrice", Float.parseFloat(m.get("preissueAvgPrice").toString()) + preissueAvgPrice);
                    }
                }
            }
        }

        //本月的 大商户发优惠卷的总数分组
        for(CardAnalyzeOutDTO clist2:cdList2){
            String datet =dateFormat.format(clist2.getCreateData());
            for (int i=0;i<list.size();i++){
                Map m =(HashMap) list.get(i);
                if(m.containsValue(datet)) {  //已经存在该日期
                    // m.get("cardKxTotal");
                    m.put("issueTotal", Integer.parseInt(m.get("issueTotal").toString()) + clist2.getIssueTotal());
                    m.put("issueMembership",Integer.parseInt(m.get("issueMembership").toString()) + clist2.getIssueMembership());
                    m.put("issueMoneyTotal",Integer.parseInt(m.get("issueMoneyTotal").toString()) + clist2.getIssueMoneyTotal()/100);
                    m.put("issueBiShu",Integer.parseInt(m.get("issueBiShu").toString()) + clist2.getIssueBiShu());
                    float issueAvgPrice=(float)Math.round((clist2.getIssueMoneyTotal()/100)/clist2.getIssueTotal()*100)/100;
                    m.put("issueAvgPrice",Float.parseFloat(m.get("issueAvgPrice").toString()) + issueAvgPrice);
                }
            }
        }
       for(int i=0;i<list.size();i++){
            Map m =(HashMap) list.get(i);
           String preCardKxTotal1 = m.get("preCardKxTotal").toString();//上个月每一天的券核销总数
           float preCardKxTotal = Float.parseFloat(preCardKxTotal1);
           
           String cardKxTotal1= m.get("cardKxTotal").toString();//本月每一天的券核销总数
           float cardKxTotal = Float.parseFloat(cardKxTotal1);

           String precard=m.get("preissueTotal").toString(); //上个月的优惠卷发放总数
           float preissueTotal = Float.parseFloat(precard);

           String issueTotal1=m.get("issueTotal").toString();   //本月的优惠卷发放总数
           float issueTotal = Float.parseFloat(issueTotal1);

           //券核销
              if(preCardKxTotal==0&&0!=cardKxTotal){  //上月的一天等于0，本月的这一天不等于0
                m.put("cardKxtqContrast",100);
                  m.put("kxTotalContrast2","+"+100);
            }else if(cardKxTotal==0&&0!=preCardKxTotal){  //当天等于0  上月的同一天不等于0
                  m.put("cardKxtqContrast",100);
                  m.put("kxTotalContrast2","-"+100);
              }
            else if(preCardKxTotal!=0&&cardKxTotal!=0) {
                  float cardKxtqContrast = ((float) (cardKxTotal - preCardKxTotal) / (float) preCardKxTotal) * 100;
                  if (cardKxTotal > preCardKxTotal) {
                      m.put("cardKxtqContrast",cardKxtqContrast);
                      m.put("kxTotalContrast2","+"+ cardKxtqContrast);
                  } else {
                      m.put("cardKxtqContrast", cardKxtqContrast);
                      m.put("kxTotalContrast2",cardKxtqContrast);
                  }
              }
                 //核销金额总计
                  String k = m.get("kxMoneyTotal").toString();  //本月或本周的
                  float kxMoneyTotalt = Float.parseFloat(k);

                  String k1 = m.get("prekxMoneyTotal").toString();  //上月或上周的
                  float prekxMoneyTotal = Float.parseFloat(k1);
                  if(prekxMoneyTotal==0&&0!=kxMoneyTotalt){
                      m.put("kxMoneyTotalContrast","+"+100);
                  }else if(kxMoneyTotalt==0&&0!=prekxMoneyTotal){
                      m.put("kxMoneyTotalContrast",-100);
                  }  else if(preCardKxTotal!=0&&cardKxTotal!=0){
                      float kxMoneyTotalContrast =((kxMoneyTotalt-prekxMoneyTotal)/prekxMoneyTotal)*100;
                      if(cardKxTotal>preCardKxTotal){
                          m.put("kxMoneyTotalContrast","+"+Math.round(kxMoneyTotalContrast*100)/100);
                      }else{
                          m.put("kxMoneyTotalContrast",Math.round(kxMoneyTotalContrast*100)/100);
                      }
              }

                //发放会员人数
                 float issueMembership= Float.parseFloat(m.get("issueMembership").toString());
                 float preissueMembership=Float.parseFloat(m.get("preissueMembership").toString());
               if(preissueMembership==0&&0!=issueMembership){
               m.put("issueMembershipContrast","+"+100);
           }else if(issueMembership==0&&0!=preissueMembership){
               m.put("issueMembershipContrast",-100);
           }  else if(preissueMembership!=0&&issueMembership!=0){
               float issueMembershipContrast =((issueMembership-preissueMembership)/preissueMembership)*100;
               if(issueMembership>preissueMembership||issueMembership==preissueMembership){
                   m.put("issueMembershipContrast","+"+Math.round(issueMembershipContrast*100)/100);
               }else{
                   m.put("issueMembershipContrast",Math.round(issueMembershipContrast*100)/100);
               }
           }

            //发放金额总计
              float issueMoneyTotal= Float.parseFloat(m.get("issueMoneyTotal").toString());
              float preissueMoneyTotal= Float.parseFloat(m.get("preissueMoneyTotal").toString());
           if(preissueMoneyTotal==0&&0!=issueMoneyTotal){
               m.put("issueMoneyTotalContrast","+"+100);
           }else if(issueMoneyTotal==0&&0!=preissueMoneyTotal){
               m.put("issueMoneyTotalContrast",-100);
           }  else if(preissueMoneyTotal!=0&&issueMoneyTotal!=0){
               float issueMoneyTotalContrast =((issueMoneyTotal-preissueMoneyTotal)/preissueMoneyTotal)*100;
               if(issueMoneyTotal>preissueMoneyTotal){
                   m.put("issueMoneyTotalContrast","+"+Math.round(issueMoneyTotalContrast*100)/100);
               }else{
                   m.put("issueMoneyTotalContrast",Math.round(issueMoneyTotalContrast*100)/100);
               }
           }

           //发放笔数
           float issueBiShu= Float.parseFloat(m.get("issueBiShu").toString());
           float preissueBiShu= Float.parseFloat(m.get("preissueBiShu").toString());
           if(preissueBiShu==0&&0!=issueBiShu){
               m.put("issueBiShuContrast","+"+100);
           }else if(issueBiShu==0&&0!=preissueBiShu){
               m.put("issueBiShuContrast",-100);
           }  else if(preissueBiShu!=0&&issueBiShu!=0){
               float issueBiShuContrast =((issueBiShu-preissueBiShu)/preissueBiShu)*100;
               if(issueMoneyTotal>preissueBiShu){
                   m.put("issueBiShuContrast","+"+Math.round(issueBiShuContrast*100)/100);
               }else{
                   m.put("issueBiShuContrast",Math.round(issueBiShuContrast*100)/100);
               }
           }
           //发放平均客单价
           float issueAvgPrice= Float.parseFloat(m.get("issueAvgPrice").toString());
           float preissueAvgPrice= Float.parseFloat(m.get("preissueAvgPrice").toString());
           if(preissueAvgPrice==0&&0!=issueAvgPrice){
               m.put("issueAvgPriceContrast","+"+100);
           }else if(issueAvgPrice==0&&0!=preissueAvgPrice){
               m.put("issueAvgPriceContrast",-100);
           }  else if(preissueAvgPrice!=0&&issueAvgPrice!=0){
               float issueAvgPriceContrast =((issueAvgPrice-preissueAvgPrice)/preissueAvgPrice)*100;
               if(issueAvgPrice>preissueAvgPrice){
                   m.put("issueAvgPriceContrast","+"+Math.round(issueAvgPriceContrast*100)/100);
               }else{
                   m.put("issueAvgPriceContrast",Math.round(issueAvgPriceContrast*100)/100);
               }
           }
         // 核销平均客单价
           float kxAvgPrice= Float.parseFloat(m.get("kxAvgPrice").toString());
           float prekxAvgPrice= Float.parseFloat(m.get("prekxAvgPrice").toString());

           if(prekxAvgPrice==0&&0!=kxAvgPrice){
               m.put("kxAvgPriceContrast","+"+100);
           }else if(kxAvgPrice==0&&0!=prekxAvgPrice){
               m.put("kxAvgPriceContrast",-100);
           }  else if(prekxAvgPrice!=0&&kxAvgPrice!=0){
               float kxAvgPriceContrast =((kxAvgPrice-prekxAvgPrice)/prekxAvgPrice)*100;
               if(kxAvgPrice>prekxAvgPrice){
                   m.put("kxAvgPriceContrast","+"+Math.round(kxAvgPriceContrast*100)/100);
               }else{
                   m.put("kxAvgPriceContrast",Math.round(kxAvgPriceContrast*100)/100);
               }
           }


             //优惠卷发放人数
           if(preissueTotal==0&&0!=issueTotal){  //上月的一天等于0，本月的这一天不等于0
               m.put("issuetqContrast",100);
              m.put("issueRenContrast","+"+100);
           }else if(issueTotal==0&&0!=preissueTotal){  //当天等于0  上月的同一天不等于0
               m.put("issuetqContrast",-100);
               m.put("issueRenContrast","-"+100);
           }
           else if(preissueTotal!=0&&issueTotal!=0) {
               float issuetqContrast = ((issueTotal - preissueTotal) / preissueTotal) * 100;
               if(issueTotal>preissueTotal){
                   m.put("issuetqContrast",(float) Math.round(issuetqContrast * 100) / 100);
                   m.put("issueRenContrast", "+"+(float) Math.round(issuetqContrast * 100) / 100);
               }else{
                   m.put("issuetqContrast",(float) Math.round(issuetqContrast * 100) / 100);
                   m.put("issueRenContrast",(float) Math.round(issuetqContrast * 100) / 100);
               }
            }
          }
        }
        cardDataOutDTO.setKxList(list);
        System.out.println(cardDataOutDTO.getKxList().toArray());

            return cardDataOutDTO;
    }

    @Override
    public CardDataOutDTO queryMonthHours(CardAnalyzeInDTO cardAnalyzeInDTO) {
        CardDataOutDTO cardDataOutDTO=new CardDataOutDTO();
        List<CardAnalyzeOutDTO> list = tradeOfflineMapper.queryMonthHours(cardAnalyzeInDTO);  //核销 时间曲线图
        List<CardAnalyzeOutDTO> list2 = tradeOfflineMapper.queryCouponIssueMonthHours(cardAnalyzeInDTO); //发放 时间曲线图
        List monthDaysHours = TwoDateDays.MonthDaysHours();
        for(CardAnalyzeOutDTO clist:list){
             for(int i=0;i<monthDaysHours.size();i++){
               Map m=(HashMap) monthDaysHours.get(i);
                 String hours = m.get("hours").toString().substring(0, 2);
                 if (hours.equals(clist.getHours())) {
                     m.put("cardKxTotal", clist.getCardKxTotal());
                     break;
                 }

             }
        }
        for(CardAnalyzeOutDTO clist2:list2){
            for(int i=0;i<monthDaysHours.size();i++){
                Map m=(HashMap) monthDaysHours.get(i);
                String hours = m.get("hours").toString().substring(0, 2);
                if (hours.equals(clist2.getHours())) {
                    m.put("issueTotal", clist2.getIssueTotal());
                    break;
                }
            }
        }
        cardDataOutDTO.setKxList(monthDaysHours);
        //cardDataOutDTO.setIssueList(monthDaysHours);
        return cardDataOutDTO;
    }

    @Override
    public CardDataOutDTO queryMonthHours2(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception{
        CardDataOutDTO cardDataOutDTO = new CardDataOutDTO();
        List<CardAnalyzeOutDTO> listCard = tradeOfflineMapper.queryMonthHours2(cardAnalyzeInDTO);
        List<CardAnalyzeOutDTO> listIssue = tradeOfflineMapper.queryCouponIssueMonthHours2(cardAnalyzeInDTO);//优惠卷发放

        int[] month_days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int year = Integer.parseInt(cardAnalyzeInDTO.getDateAnalye().split("-")[0]);
        int month = Integer.parseInt(cardAnalyzeInDTO.getDateAnalye().split("-")[1]);
        int days = 0;
        if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) {//闰2月
            days = 29;
        } else {
            days = month_days[month];
        }
          List list= null;

        String firstMonth = TwoDateDays.getFirstMonth(cardAnalyzeInDTO.getDateAnalye(), "yyyy-MM", "yyyy-MM-dd");//获取某月的第一天
        String lastMonth = TwoDateDays.getLastMonth(cardAnalyzeInDTO.getDateAnalye(), "yyyy-MM", "yyyy-MM-dd");//获取某月的最后一天
       // DateUtil.fomatDate(cardAnalyzeInDTO.getDateAnalye());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String dateAnalye = cardAnalyzeInDTO.getDateAnalye();
        long time = sdf.parse(dateAnalye).getTime();
        long time2 = new Date().getTime();
        if(cardAnalyzeInDTO.getDateAnalye().equals(DateUtil.fomatDate(new Date(), DateUtil.sdfMonth))){ //如果前台选择传入过来的日期等于当前日期  数据就只构造到当前日期的几号
            list =TwoDateDays.TwoDateBetweenDays2(firstMonth,DateUtil.fomatDate(new Date(), DateUtil.sdfDay));
        }
         else if(time>time2){
            list=new ArrayList();
        }
        else{
            list =TwoDateDays.TwoDateBetweenDays2(firstMonth,lastMonth);
        }

        for(int i=0;i<list.size();i++){
           Map m=(HashMap) list.get(i);
           m.put("hoursCardTotal",TwoDateDays.MonthDaysHours());  //构造24个小时时间段的list<Map<String,Object>>
        }
        List kxissue = new ArrayList();
       if(list.size()>0){
              Map map = new HashMap();
              map.put("kxIssueZb",TwoDateDays.MonthDaysHours());
              kxissue.add(map);
           for(CardAnalyzeOutDTO cList:listCard){
               String hours = cList.getHours().substring(cList.getHours().length() - 2);  //小时
               String dateTime = cList.getHours().substring(0,10);  //日期 年-月-日
               for(int j=0;j<list.size();j++){
                   Map m =(HashMap)list.get(j);
                   if(dateTime.equals(m.get("createTime").toString())){ //首先日期是对应的
                       List list2 =(ArrayList)m.get("hoursCardTotal");         // List<Map<string,<List<Map<String,Object>>>>>  格式的数据
                       for(int i=0;i<list2.size();i++){
                           Map mm=(HashMap)list2.get(i);
                           String hour = mm.get("hours").toString().substring(0, 2);
                           if(hours.equals(hour)){                        //数据库查出的小时对应构造24小时数据的小时时间段
                               mm.put("cardKxTotal",cList.getCardKxTotal());
                              /* float zb = (float) cList.getCardKxTotal() / (float) cList.getTotal();
                               mm.put("zb",(float)Math.round(zb*100)/100);*/
                               break;
                           }

                       }
                   }

               }
               //算出一个月中 每个时间段(24小时)的总占比

               for(int i=0;i<kxissue.size();i++){
                   Map m=(HashMap)kxissue.get(i);

                   List list3 =(ArrayList)m.get("kxIssueZb"); //占比
                   for(int j=0;j<list3.size();j++){
                       Map mm2=(HashMap)list3.get(j);
                       String hour = mm2.get("hours").toString().substring(0, 2);
                       if(hours.equals(hour)){
                           float zb = Float.parseFloat(mm2.get("zb").toString())+((float) cList.getCardKxTotal() / (float) cList.getTotal())*100;
                           mm2.put("zb",+(float)Math.round(zb*100)/100);
                        }
                  }
               }
           }
           for(CardAnalyzeOutDTO listIssue2:listIssue){
               String issueHours = listIssue2.getHours().substring(listIssue2.getHours().length() - 2);  //小时
               String issueDateTime = listIssue2.getHours().substring(0,10);  //日期 年-月-日
               for(int j=0;j<list.size();j++){
                   Map m =(HashMap)list.get(j);
                   if(issueDateTime.equals(m.get("createTime").toString())){ //首先日期是对应的
                       List list2 =(ArrayList)m.get("hoursCardTotal");         // List<Map<string,<List<Map<String,Object>>>>>  格式的数据
                       for(int i=0;i<list2.size();i++){
                           Map mm=(HashMap)list2.get(i);
                           String hour = mm.get("hours").toString().substring(0, 2);
                           if(issueHours.equals(hour)){                        //数据库查出的小时对应构造24小时数据的小时时间段
                               mm.put("issueTotal",listIssue2.getIssueTotal());
                               break;
                           }
                       }
                   }
               }
               //算出一个月中 每个时间段(24小时)的总占比

             for(int i=0;i<kxissue.size();i++){
                   Map m=(HashMap)kxissue.get(i);
                   List list3 =(ArrayList)m.get("kxIssueZb"); //占比
                   for(int j=0;j<list3.size();j++){
                       Map mm2=(HashMap)list3.get(j);
                       String hour = mm2.get("hours").toString().substring(0, 2);
                       if(issueHours.equals(hour)){
                           float zb2 = Float.parseFloat(mm2.get("zb2").toString())+((float) listIssue2.getIssueTotal() / (float) listIssue2.getTotalNumber())*100;
                           mm2.put("zb2",+(float)Math.round(zb2*100)/100);
                       }
                   }
               }
           }
       }
        cardDataOutDTO.setKxList(list);
        cardDataOutDTO.setObjList(kxissue);
        System.out.println(kxissue);
        return cardDataOutDTO;

    }

    @Override
    public CardAnalyzeOutDTO menWomenProportion(CardAnalyzeInDTO cardAnalyzeInDTO) {
        CardAnalyzeOutDTO cardAnalyzeOutDTO = tradeOfflineMapper.menWomenProportion(cardAnalyzeInDTO);
        return cardAnalyzeOutDTO;
    }

    @Override
    public CardDataOutDTO queryAgeGroupBy(CardAnalyzeInDTO cardAnalyzeInDTO) {
        CardDataOutDTO cardDataOutDTO=new CardDataOutDTO();
        List list = new ArrayList();
        Map m = new HashMap();
        m.put("0-19",0);
        m.put("20-29",0);
        m.put("30-39",0);
        m.put("40-49",0);
        m.put("50-59",0);
        m.put("d60",0);
        m.put("wz",0);
        List<CardAnalyzeOutDTO> list2 = tradeOfflineMapper.queryAgeGroupBy(cardAnalyzeInDTO);
        for(CardAnalyzeOutDTO cdList: list2){
            String strdate = cdList.getIdNo().substring(6, 14);  //身份证截取的日期字符串
            System.out.println(strdate);
            Date idNoDate = DateUtil.fomatDate(strdate, DateUtil.sdfDays);  //身份证日期
            Date nowDate = new Date();                          //现在的时间
            long year;
            if(strdate.equals("00000000")&&null!=cdList.getBirthday()){//身份证为空 但生日不为空  就取生日
                String birthday = cdList.getBirthday();
                Date birthdayDate = DateUtil.fomatDate(birthday, DateUtil.sdfDays);
                 year =(nowDate.getTime() - birthdayDate.getTime()) / (24 * 60 * 60 * 1000)/365;  //得到相差多少年
            }else{
                 year =(nowDate.getTime() - idNoDate.getTime()) / (24 * 60 * 60 * 1000)/365;  //得到相差多少年
            }
            if(strdate.equals("00000000")&&null==cdList.getBirthday()){
                m.put("wz",Integer.parseInt(m.get("wz").toString())+1);
            }else{
                if(year>=0&&year<=19){
                    m.put("0-19",Integer.parseInt(m.get("0-19").toString())+1);
                }else if(year>=20&&year<=29){
                    m.put("20-29",Integer.parseInt(m.get("20-29").toString())+1);
                } else if(year>=30&&year<=39){
                    m.put("30-39",Integer.parseInt(m.get("30-39").toString())+1);
                }else if(year>=40&&year<=49){
                    m.put("40-49",Integer.parseInt(m.get("40-49").toString())+1);
                }else if(year>=50&&year<=59){
                    m.put("50-59",Integer.parseInt(m.get("50-59").toString())+1);
                }else if(year>=60){
                    m.put("d60",Integer.parseInt(m.get("d60").toString())+1);
                }
            }
             System.out.println(year);
            }

              list.add(m);
        System.out.println(list);

        System.out.println(m);

        Map map=(HashMap)list.get(0);
        if(list2.size()>0||list2!=null){
            cardDataOutDTO.setAge1((float)Math.round(((Float.parseFloat(map.get("0-19").toString())/list2.size())*100)*100)/100);
            cardDataOutDTO.setAge2((float)Math.round(((Float.parseFloat(map.get("20-29").toString())/list2.size())*100)*100)/100);
            cardDataOutDTO.setAge3((float)Math.round(((Float.parseFloat(map.get("30-39").toString())/list2.size())*100)*100)/100);
            cardDataOutDTO.setAge4((float)Math.round(((Float.parseFloat(map.get("40-49").toString())/list2.size())*100)*100)/100);
            cardDataOutDTO.setAge5((float)Math.round(((Float.parseFloat(map.get("50-59").toString())/list2.size())*100)*100)/100);
            cardDataOutDTO.setAge6((float)Math.round(((Float.parseFloat(map.get("d60").toString())/list2.size())*100)*100)/100);
            cardDataOutDTO.setAge7((float)Math.round(((Float.parseFloat(map.get("wz").toString())/list2.size())*100)*100)/100);
            cardDataOutDTO.setTotal(list2.size());
        }else{
            cardDataOutDTO.setAge1(0f);
            cardDataOutDTO.setAge2(0f);
            cardDataOutDTO.setAge3(0f);
            cardDataOutDTO.setAge4(0f);
            cardDataOutDTO.setAge5(0f);
            cardDataOutDTO.setAge6(0f);
            cardDataOutDTO.setAge7(0f);
        }

        //cardDataOutDTO.setKxList(list);
        return cardDataOutDTO;

    }

    @Override
    public CardDataOutDTO queryIssueAgeGroupBy(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception {
        CardDataOutDTO issueDataOutDTO=new CardDataOutDTO();
        List list = new ArrayList();
        Map m2 = new HashMap();
        m2.put("0-19",0);
        m2.put("20-29",0);
        m2.put("30-39",0);
        m2.put("40-49",0);
        m2.put("50-59",0);
        m2.put("d60",0);
        m2.put("wz",0);
        List<CardAnalyzeOutDTO> list2 = tradeOfflineMapper.queryIssueAgeGroupBy(cardAnalyzeInDTO);
        for(CardAnalyzeOutDTO cdList: list2){
            String strdate =cdList.getIdNo();
            System.out.println(strdate);
            Date idNoDate = DateUtil.fomatDate(strdate, DateUtil.sdfDays);
            Date nowDate = new Date();                          //现在的时间
            long year;
            if(strdate.equals("00000000")&&null!=cdList.getBirthday()){//身份证为空 但生日不为空  就取生日
                String birthday = cdList.getBirthday();
                Date birthdayDate = DateUtil.fomatDate(birthday, DateUtil.sdfDays);
                year =(nowDate.getTime() - birthdayDate.getTime()) / (24 * 60 * 60 * 1000)/365;  //得到相差多少年
            }else{
                year =(nowDate.getTime() - idNoDate.getTime()) / (24 * 60 * 60 * 1000)/365;  //得到相差多少年
            }
            if(strdate.equals("00000000")&&null==cdList.getBirthday()){
                m2.put("wz",Integer.parseInt(m2.get("wz").toString())+1);
            }else{
                if(year>=0&&year<=19){
                    m2.put("0-19",Integer.parseInt(m2.get("0-19").toString())+1);
                }else if(year>=20&&year<=29){
                    m2.put("20-29",Integer.parseInt(m2.get("20-29").toString())+1);
                } else if(year>=30&&year<=39){
                    m2.put("30-39",Integer.parseInt(m2.get("30-39").toString())+1);
                }else if(year>=40&&year<=49){
                    m2.put("40-49",Integer.parseInt(m2.get("40-49").toString())+1);
                }else if(year>=50&&year<=59){
                    m2.put("50-59",Integer.parseInt(m2.get("50-59").toString())+1);
                }else if(year>=60){
                    m2.put("d60",Integer.parseInt(m2.get("d60").toString())+1);
                }
            }
        }
        list.add(m2);
        Map map=(HashMap)list.get(0);
        if(list2.size()>0||list2!=null){
            issueDataOutDTO.setAge1((float)Math.round(((Float.parseFloat(map.get("0-19").toString())/list2.size())*100)*100)/100);
            issueDataOutDTO.setAge2((float)Math.round(((Float.parseFloat(map.get("20-29").toString())/list2.size())*100)*100)/100);
            issueDataOutDTO.setAge3((float)Math.round(((Float.parseFloat(map.get("30-39").toString())/list2.size())*100)*100)/100);
            issueDataOutDTO.setAge4((float)Math.round(((Float.parseFloat(map.get("40-49").toString())/list2.size())*100)*100)/100);
            issueDataOutDTO.setAge5((float)Math.round(((Float.parseFloat(map.get("50-59").toString())/list2.size())*100)*100)/100);
            issueDataOutDTO.setAge6((float)Math.round(((Float.parseFloat(map.get("d60").toString())/list2.size())*100)*100)/100);
            issueDataOutDTO.setAge7((float)Math.round(((Float.parseFloat(map.get("wz").toString())/list2.size())*100)*100)/100);
            issueDataOutDTO.setTotal(list2.size());
        }else{
            issueDataOutDTO.setAge1(0f);
            issueDataOutDTO.setAge2(0f);
            issueDataOutDTO.setAge3(0f);
            issueDataOutDTO.setAge4(0f);
            issueDataOutDTO.setAge5(0f);
            issueDataOutDTO.setAge6(0f);
            issueDataOutDTO.setAge7(0f);
        }
        return issueDataOutDTO;
    }


    @Override
    public CardAnalyzeOutDTO queryMembersTotal(CardAnalyzeInDTO cardAnalyzeInDTO) {
        CardAnalyzeOutDTO cardAnalyzeOutDTO = new CardAnalyzeOutDTO();
       // cardAnalyzeInDTO.setQueryType(1);
        CardAnalyzeOutDTO membersTotal = tradeOfflineMapper.queryMembersTotal(cardAnalyzeInDTO);//会员总人数

        CardAnalyzeInDTO cardAnalyzeInDTO2 = new CardAnalyzeInDTO();
        cardAnalyzeInDTO2.setGroupId(cardAnalyzeInDTO.getGroupId());
        cardAnalyzeInDTO2.setQueryType(2);
        cardAnalyzeInDTO2.setDateAnalye(cardAnalyzeInDTO.getDateAnalye());
        CardAnalyzeOutDTO newMembersTotal = tradeOfflineMapper.queryMembersTotal(cardAnalyzeInDTO2); //新会员总人数

        cardAnalyzeOutDTO.setMembersTotal(membersTotal.getMembersTotal());  //会员总人数

        cardAnalyzeOutDTO.setNewMembersTotal(newMembersTotal.getMembersTotal()); //新会员总人数
        cardAnalyzeOutDTO.setOldMembersTotal(membersTotal.getMembersTotal()-newMembersTotal.getMembersTotal());//老会员总人数
        return cardAnalyzeOutDTO;
    }

    @Override
    public CardAnalyzeOutDTO queryIssueMembersTotal(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception {
        CardAnalyzeOutDTO cardAnalyzeOutDTO = new CardAnalyzeOutDTO();
        //cardAnalyzeInDTO.setQueryType(1);
        CardAnalyzeOutDTO membersTotal = tradeOfflineMapper.queryIssueMembersTotal(cardAnalyzeInDTO);//会员总人数

        CardAnalyzeInDTO cardAnalyzeInDTO2 = new CardAnalyzeInDTO();
        cardAnalyzeInDTO2.setGroupId(cardAnalyzeInDTO.getGroupId());
        cardAnalyzeInDTO2.setQueryType(2);
        cardAnalyzeInDTO2.setDateAnalye(cardAnalyzeInDTO.getDateAnalye());
        CardAnalyzeOutDTO newMembersTotal = tradeOfflineMapper.queryIssueMembersTotal(cardAnalyzeInDTO2); //新会员总人数
        cardAnalyzeOutDTO.setMembersTotal(membersTotal.getMembersTotal());  //会员总人数
        cardAnalyzeOutDTO.setNewMembersTotal(newMembersTotal.getMembersTotal()); //新会员总人数
        cardAnalyzeOutDTO.setOldMembersTotal(membersTotal.getMembersTotal()-newMembersTotal.getMembersTotal());//老会员总人数
        return cardAnalyzeOutDTO;
    }

    @Override
    public CardDataOutDTO queryCoupon(CardAnalyzeInDTO cardAnalyzeInDTO) {
        List dataList = new ArrayList();
        CardDataOutDTO cardDataOutDTO = new CardDataOutDTO();
        cardAnalyzeInDTO.setQueryType(1);  //查询类型为1时就分页   //查上月的数据是不需要分页的
        List<CardAnalyzeOutDTO> kxList = tradeOfflineMapper.queryCoupon(cardAnalyzeInDTO); //本月
        List listKxData = new ArrayList();
          for(CardAnalyzeOutDTO kxlist:kxList){
              Map m = new HashMap();
              m.put("cardKxTotalContrast","+100");
              m.put("kxMoneyTotalContrast","+100");
              m.put("kxMembershipContrast","+100");
              m.put("orderTotalMoneyContrast","+100");
              m.put("kxAvgPriceContrast","+100");

              m.put("couponName",kxlist.getCouponName());
              m.put("couponType",kxlist.getCouponType());
              m.put("couponValue",kxlist.getCouponValue());
              m.put("cardKxTotal",kxlist.getCardKxTotal());
              m.put("kxMoneyTotal",kxlist.getKxMoneyTotal());
              m.put("issueMembership",kxlist.getIssueMembership());
              m.put("orderTotalMoney",kxlist.getOrderTotalMoney());
              float kxAvgPrice = (kxlist.getOrderTotalMoney()/100) / kxlist.getCardKxTotal();
              m.put("kxAvgPrice",(float)Math.round(kxAvgPrice*100)/100);


              m.put("precardKxTotal",0);
              m.put("prekxMoneyTotal",0);
              m.put("preissueMembership",0);
              m.put("preorderTotalMoney",0);
              m.put("prekxAvgPrice",0);
              listKxData.add(m);
          }
        //上月
        CardAnalyzeInDTO precdin = new CardAnalyzeInDTO();
        precdin.setGroupId(cardAnalyzeInDTO.getGroupId());
        precdin.setDateAnalye(TwoDateDays.preDateTime(cardAnalyzeInDTO.getDateAnalye()));
        List<CardAnalyzeOutDTO> prekxList = tradeOfflineMapper.queryCoupon(precdin);
        if(kxList!=null||kxList.size()>0) {    //本月有数据  才去存储上个月的数据  这里要显示上个月所有的数据
          for(CardAnalyzeOutDTO prelist:prekxList){
              for(int i=0;i<listKxData.size();i++){
                  Map m=(HashMap)listKxData.get(i);
                  String couponName= m.get("couponName").toString();  //本月数据中优惠卷的名称
                  String precouponName= prelist.getCouponName(); //上月数据中优惠卷的名称
                  if(couponName.trim().equals(precouponName.trim())){
                      m.put("precardKxTotal",prelist.getCardKxTotal());
                      m.put("prekxMoneyTotal",prelist.getKxMoneyTotal());
                      m.put("preissueMembership", prelist.getIssueMembership());
                      m.put("preorderTotalMoney",prelist.getOrderTotalMoney());
                      float prekxAvgPrice = (prelist.getOrderTotalMoney()/100) / prelist.getCardKxTotal();
                      m.put("prekxAvgPrice",(float)Math.round(prekxAvgPrice*100)/100);
                  }
              }
          }
        }
        for(int i=0;i<listKxData.size();i++) {
            Map m = (HashMap) listKxData.get(i);
            //核销张数对比
          int cardKxTotal= Integer.parseInt(m.get("cardKxTotal").toString());
          int precardKxTotal= Integer.parseInt(m.get("precardKxTotal").toString());
            if(cardKxTotal==0&&precardKxTotal!=0){
                m.put("cardKxTotalContrast","-"+100);
            }else if(cardKxTotal!=0&&precardKxTotal!=0){
                float cardKxTotalContrast =((float) (cardKxTotal - precardKxTotal) / (float) precardKxTotal)*100;
                if(cardKxTotal>precardKxTotal){
                    m.put("cardKxTotalContrast","+"+(float)Math.round(cardKxTotalContrast*100)/100);
                }else {
                    m.put("cardKxTotalContrast", (float) Math.round(cardKxTotalContrast * 100) / 100);
                }
            }

            //核销优惠总额对比
            float kxMoneyTotal= Float.parseFloat(m.get("cardKxTotal").toString());
            float prekxMoneyTotal= Float.parseFloat(m.get("prekxMoneyTotal").toString());
            if(kxMoneyTotal==0&&prekxMoneyTotal!=0){
                m.put("kxMoneyTotalContrast","-"+100);
            }else if(kxMoneyTotal!=0&&prekxMoneyTotal!=0){
                float kxMoneyTotalContrast =((kxMoneyTotal - prekxMoneyTotal)/ prekxMoneyTotal)*100;
                if(kxMoneyTotal>prekxMoneyTotal){
                    m.put("kxMoneyTotalContrast","+"+(float)Math.round(kxMoneyTotalContrast*100)/100);
                }else{
                    m.put("kxMoneyTotalContrast",(float)Math.round(kxMoneyTotalContrast*100)/100);
                }

            }

            //核销会员人数对比
            int issueMembership= Integer.parseInt(m.get("issueMembership").toString());
            int preissueMembership= Integer.parseInt(m.get("preissueMembership").toString());
            if(issueMembership==0&&preissueMembership!=0){
                m.put("kxMembershipContrast","-"+100);
            }else if(issueMembership!=0&&preissueMembership!=0){
                float kxMembershipContrast =((float) (issueMembership - preissueMembership) / (float) preissueMembership)*100;
                if(issueMembership>preissueMembership){
                    m.put("kxMembershipContrast","+"+(float)Math.round(kxMembershipContrast*100)/100);
                }else {
                    m.put("kxMembershipContrast", (float) Math.round(kxMembershipContrast * 100) / 100);
                }
            }

            //订单总额对比
            float orderTotalMoney= Float.parseFloat(m.get("orderTotalMoney").toString());
            float preorderTotalMoney= Float.parseFloat(m.get("preorderTotalMoney").toString());
            if(orderTotalMoney==0&&preorderTotalMoney!=0){
                m.put("orderTotalMoneyContrast","-"+100);
            }else if(orderTotalMoney!=0&&preorderTotalMoney!=0){
                float orderTotalMoneyContrast =((orderTotalMoney - preorderTotalMoney)/ preorderTotalMoney)*100;
                if(orderTotalMoney>preorderTotalMoney){
                    m.put("orderTotalMoneyContrast","+"+(float)Math.round(orderTotalMoneyContrast*100)/100);
                }else {
                    m.put("orderTotalMoneyContrast", (float) Math.round(orderTotalMoneyContrast * 100) / 100);
                }
            }

            //客单价对比
            float kxAvgPrice= Float.parseFloat(m.get("kxAvgPrice").toString());
            float prekxAvgPrice= Float.parseFloat(m.get("prekxAvgPrice").toString());
            if(kxAvgPrice==0&&prekxAvgPrice!=0){
                m.put("kxAvgPriceContrast","-"+100);
            }else if(kxAvgPrice!=0&&prekxAvgPrice!=0){
                float kxAvgPriceContrast =((kxAvgPrice - prekxAvgPrice)/ prekxAvgPrice)*100;
                if(kxAvgPrice>prekxAvgPrice){
                    m.put("kxAvgPriceContrast","+"+(float)Math.round(kxAvgPriceContrast*100)/100);
                }else {
                    m.put("kxAvgPriceContrast", (float) Math.round(kxAvgPriceContrast * 100) / 100);
                }
            }

        }
        cardDataOutDTO.setKxList(listKxData);



        List<CardAnalyzeOutDTO> issueList = tradeOfflineMapper.queryIssueCoupon(cardAnalyzeInDTO);//本月
        for(CardAnalyzeOutDTO cList:issueList){
            Map m = new HashMap();
            m.put("issueTotalContrast","+100");
            m.put("issueMoneyTotalContrast","+100");
            m.put("issueBiShuContrast","+100");
            m.put("issueMembershipContrast","+100");
            m.put("orderTotalMoneyContrast","+100");
            m.put("issueAvgPriceContrast","+100");

            m.put("issueTotal",cList.getIssueTotal());
            m.put("issueMoneyTotal",cList.getIssueMoneyTotal());
            m.put("issueBiShu",cList.getIssueBiShu());
            m.put("issueMembership",cList.getIssueMembership());
            m.put("orderTotalMoney",(float)Math.round(((cList.getOrderTotalMoney()/100)/100)*100));
           // m.put("issueAvgPrice",cList.getIssueAvgPrice());
            m.put("preissueTotal",0);
            m.put("preissueMoneyTotal",0);
            m.put("preissueBiShu",0);
            m.put("preissueMembership",0);
            m.put("preorderTotalMoney",0);
          //  m.put("preissueAvgPrice",0);
            m.put("prekdPrice",0);

            m.put("couponName",cList.getCouponName());
            m.put("couponValue",cList.getCouponValue());
            m.put("couponType",cList.getCouponType());
            m.put("kdPrice",(float)Math.round(((cList.getIssueMoneyTotal()/100)/cList.getIssueTotal())*100)/100);//客单价
            //cList.setKdPrice();
            dataList.add(m);
        }
        //上月
        CardAnalyzeInDTO cdin = new CardAnalyzeInDTO();
        cdin.setGroupId(cardAnalyzeInDTO.getGroupId());
        cdin.setDateAnalye(TwoDateDays.preDateTime(cardAnalyzeInDTO.getDateAnalye()));
        List<CardAnalyzeOutDTO> preIssueList = tradeOfflineMapper.queryIssueCoupon(cdin);//上月
        if(issueList!=null||issueList.size()>0){    //本月有数据  才去存储上个月的数据
            for(CardAnalyzeOutDTO preList:preIssueList){
                 for(int i=0;i<dataList.size();i++){
                     Map m=(HashMap)dataList.get(i);
                    String couponName= m.get("couponName").toString();  //本月数据中优惠卷的名称
                     String precouponName= preList.getCouponName(); //上月数据中优惠卷的名称
                     if(couponName.trim().equals(precouponName.trim())){
                         m.put("preissueTotal",preList.getIssueTotal());
                         m.put("preissueMoneyTotal",preList.getIssueMoneyTotal());
                         m.put("preissueBiShu",preList.getIssueBiShu());
                         m.put("preissueMembership",preList.getIssueMembership());
                         m.put("preorderTotalMoney",(float)Math.round(((preList.getOrderTotalMoney()/100)/100)*100));
                         //m.put("preissueAvgPrice",preList.getIssueAvgPrice());
                         m.put("prekdPrice",(float)Math.round(((preList.getIssueMoneyTotal()/100)/preList.getIssueTotal())*100)/100);//客单价
                     }
                 }
            }
        }

        for(int i=0;i<dataList.size();i++){
            Map m=(HashMap)dataList.get(i);
            //发放张数对比数据
            int issueTotal = Integer.parseInt(m.get("issueTotal").toString());
            int preissueTotal = Integer.parseInt(m.get("preissueTotal").toString());
            if(issueTotal==0&&preissueTotal!=0){
                m.put("issueTotalContrast","-"+100);
            }else if(issueTotal!=0&&preissueTotal!=0){
                float issueTotalContrast =((float) (issueTotal - preissueTotal) / (float) preissueTotal)*100;
                if(issueTotal>preissueTotal){
                    m.put("issueTotalContrast","+"+(float)Math.round(issueTotalContrast*100)/100);
                }
                m.put("issueTotalContrast",(float)Math.round(issueTotalContrast*100)/100);
            }
            //发放券总额对比数据
            float issueMoneyTotal = Float.parseFloat(m.get("issueMoneyTotal").toString());
            float preissueMoneyTotal = Float.parseFloat(m.get("preissueMoneyTotal").toString());
            if(issueMoneyTotal==0&&preissueMoneyTotal!=0){
                m.put("issueMoneyTotalContrast","-"+100);
            }else if(issueMoneyTotal!=0&&preissueMoneyTotal!=0){
                float issueMoneyTotalContrast =((issueMoneyTotal - preissueMoneyTotal) / preissueMoneyTotal)*100;
                if(issueMoneyTotal>preissueMoneyTotal){
                    m.put("issueMoneyTotalContrast","+"+(float)Math.round(issueMoneyTotalContrast*100)/100);
                }
                m.put("issueMoneyTotalContrast",(float)Math.round(issueMoneyTotalContrast*100)/100);
            }

            //发放笔数对比数据
            int issueBiShu =Integer.parseInt(m.get("issueBiShu").toString());
            int preissueBiShu = Integer.parseInt(m.get("preissueBiShu").toString());
            if(issueBiShu==0&&preissueBiShu!=0){
                m.put("issueBiShuContrast","-"+100);
            }else if(issueBiShu!=0&&preissueBiShu!=0){
                float issueBiShuTotalContrast =((float) (issueBiShu - preissueBiShu) / (float) preissueBiShu)*100;
                if(issueBiShu>preissueBiShu){
                    m.put("issueBiShuContrast","+"+(float)Math.round(issueBiShuTotalContrast*100)/100);
                }
                m.put("issueBiShuContrast",(float)Math.round(issueBiShuTotalContrast*100)/100);
            }
            //发放会员数对比数据
            int issueMembership =Integer.parseInt(m.get("issueMembership").toString());
            int preissueMembership = Integer.parseInt(m.get("preissueMembership").toString());
            if(issueMembership==0&&preissueMembership!=0){
                m.put("issueMembershipContrast","-"+100);
            }else if(issueMembership!=0&&preissueMembership!=0){
                float issueMembershipContrast =((float) (issueMembership - preissueMembership) / (float) preissueMembership)*100;
                if(issueMembership>preissueMembership){
                    m.put("issueMembershipContrast","+"+(float)Math.round(issueMembershipContrast*100)/100);
                }else{
                    m.put("issueMembershipContrast",(float)Math.round(issueMembershipContrast*100)/100);
                }

            }

            //订单总额对比数据
            float orderTotalMoney =Float.parseFloat(m.get("orderTotalMoney").toString());
            float preorderTotalMoney = Float.parseFloat(m.get("preorderTotalMoney").toString());
            if(orderTotalMoney==0&&preorderTotalMoney!=0){
                m.put("orderTotalMoneyContrast","-"+100);
            }else if(orderTotalMoney!=0&&preorderTotalMoney!=0){
                float orderTotalMoneyContrast =((orderTotalMoney - preorderTotalMoney)/preorderTotalMoney)*100;
                if(orderTotalMoney>preorderTotalMoney){
                    m.put("orderTotalMoneyContrast","+"+(float)Math.round(orderTotalMoneyContrast*100)/100);
                }else{
                    m.put("orderTotalMoneyContrast",(float)Math.round(orderTotalMoneyContrast*100)/100);
                }
            }else if(orderTotalMoney==0&&preorderTotalMoney==0){
                m.put("orderTotalMoneyContrast","+"+0);
            }

            //客单价对比数据
            float kdPrice =Float.parseFloat(m.get("kdPrice").toString());
            float prekdPrice = Float.parseFloat(m.get("prekdPrice").toString());
            if(kdPrice==0&&prekdPrice!=0){
                m.put("issueAvgPriceContrast","-"+100);
            }else if(kdPrice!=0&&prekdPrice!=0){
                float orderTotalMoneyContrast =((kdPrice - prekdPrice)/prekdPrice)*100;
                if(kdPrice>prekdPrice){
                    m.put("issueAvgPriceContrast","+"+(float)Math.round(orderTotalMoneyContrast*100)/100);
                }else{
                    m.put("issueAvgPriceContrast",(float)Math.round(orderTotalMoneyContrast*100)/100);
                }
            }
        }

        cardDataOutDTO.setIssueList(dataList);
        return cardDataOutDTO;
    }

    @Override
    public CardDataOutDTO queryMoneyGroupBy(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception {
        CardDataOutDTO cardDataOutDTO = new CardDataOutDTO();

        List list = new ArrayList();
        Map m = new HashMap();
        m.put("count", 0);  //总数
        list.add(m);
        List<CardAnalyzeOutDTO> list2 = tradeOfflineMapper.queryMoneyGroupBy(cardAnalyzeInDTO);
        for(CardAnalyzeOutDTO cdList:list2){
            Integer total = cdList.getTotal();
            m.put("count", Integer.parseInt(m.get("count").toString())+total);  //得出总数
           // String moneyInterval = cdList.getMoneyInterval();
        }
        for(CardAnalyzeOutDTO cdList2:list2){
            if("0元-50元".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion1((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }else if("50元-100元".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion2((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }else if("100元-200元".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion3((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }else if("200元-500元".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion4((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }else if("500元-1000元".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion5((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }else if("1000元-2000元".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion6((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }else if("2000元以上".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion7((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }

        }
        System.out.println(cardDataOutDTO);
        return cardDataOutDTO;
    }

    @Override
    public CardDataOutDTO queryIssueMoneyGroupBy(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception {
        CardDataOutDTO cardDataOutDTO = new CardDataOutDTO();
        List list = new ArrayList();
        Map m = new HashMap();
        m.put("count", 0);  //总数
        list.add(m);
        List<CardAnalyzeOutDTO> list2 = tradeOfflineMapper.queryIssueMoneyGroupBy(cardAnalyzeInDTO);
        for(CardAnalyzeOutDTO cdList:list2){
            Integer total = cdList.getTotal();
            m.put("count", Integer.parseInt(m.get("count").toString())+total);  //得出总数
        }
        for(CardAnalyzeOutDTO cdList2:list2){
            if("0元-50元".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion1((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }else if("50元-100元".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion2((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }else if("100元-200元".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion3((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }else if("200元-500元".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion4((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }else if("500元-1000元".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion5((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }else if("1000元-2000元".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion6((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }else if("2000元以上".equals(cdList2.getMoneyInterval())){
                cardDataOutDTO.setMoneryProportion7((float)Math.round(((float)cdList2.getTotal()/Float.parseFloat(m.get("count").toString())*100)*100)/100);
            }
        }
        return cardDataOutDTO;
    }

    @Override
    public CardAnalyzeOutDTO querymenIssueWomen(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception {
        return tradeOfflineMapper.querymenIssueWomen(cardAnalyzeInDTO);
    }

    @Override
    public CardDataOutDTO queryindustryCouponStatistical(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception {
        CardDataOutDTO cardDataOutDTO = new CardDataOutDTO();
        List list = new ArrayList();
        //本月的
        List<CardAnalyzeOutDTO> kxList = tradeOfflineMapper.queryindustryCouponStatistical(cardAnalyzeInDTO); //行业 卡券核销
        List<CardAnalyzeOutDTO> issueList = tradeOfflineMapper.queryindustryCouponIssueStatistical(cardAnalyzeInDTO); //行业 卡券发放

        //上月的
        CardAnalyzeInDTO cdin = new CardAnalyzeInDTO();
        cdin.setGroupId(cardAnalyzeInDTO.groupId);
        cdin.setDateAnalye(TwoDateDays.preDateTime(cardAnalyzeInDTO.getDateAnalye()));
        cdin.setQueryType(cardAnalyzeInDTO.getQueryType());
        List<CardAnalyzeOutDTO> prekxList = tradeOfflineMapper.queryindustryCouponStatistical(cdin); //行业 卡券核销
        List<CardAnalyzeOutDTO> preissueList = tradeOfflineMapper.queryindustryCouponIssueStatistical(cdin); //行业 卡券发放

       /* Map map = new HashMap();
        map.put("count",0);*/

       if(cardAnalyzeInDTO.getQueryType()==1){  //处理行业的数据
           for(CardAnalyzeOutDTO kxlist:kxList){
               Map m = new HashMap();
               m.put("industryID",kxlist.getIndustryID());
               m.put("industryName",kxlist.getIndustryName());
               m.put("merchantName",kxlist.getMerchantName());
               m.put("cardKxTotal",kxlist.getCardKxTotal());
               m.put("kxMoneyTotal",kxlist.getKxMoneyTotal());
               m.put("issueTotal",0);
               m.put("issueMembership", 0);

               m.put("precardKxTotal",0);
               m.put("prekxMoneyTotal",0);
               m.put("preissueTotal",0);
               m.put("preissueMembership",0);

               m.put("cardKxTotalContrast",0);
               m.put("kxMoneyTotalContrast",0);
               m.put("issueTotalContrast",0);
               m.put("issueMembershipContrast",0);

               m.put("total",kxlist.getTotal()); //本月总数
               m.put("totalContrast", 0);

               m.put("industryIssuetotal",0);      //卡券发放总数
               m.put("industryIssuetotalContrast",0);//每个行业卡券发放总数占比
               if(prekxList!=null||prekxList.size()>0){
                   for(CardAnalyzeOutDTO preList : prekxList){
                       if(kxlist.getIndustryName().equals(preList.getIndustryName())){  //本月的数据 在上个月的存在 就放入上个月的数据
                           m.put("precardKxTotal",preList.getCardKxTotal());
                           m.put("prekxMoneyTotal",preList.getKxMoneyTotal());
                       }else{  //否则 就是0
                           m.put("precardKxTotal",0);
                           m.put("prekxMoneyTotal",0);
                       }
                   }
               }
               if(issueList!=null||issueList.size()>0){   //卡券发放
                   for(CardAnalyzeOutDTO issuelist:issueList){   //本月
                       if(kxlist.getIndustryName().equals(issuelist.getIndustryName())){
                           m.put("issueTotal",issuelist.getIssueTotal());
                           m.put("issueMembership", issuelist.getIssueMembership());
                           m.put("industryIssuetotal",issuelist.getIndustryIssuetotal());
                           if(preissueList!=null||preissueList.size()>0){  //上月
                               for(CardAnalyzeOutDTO dataList:preissueList){
                                   if(issuelist.getIndustryName().equals(dataList.getIndustryName())){
                                       m.put("preissueTotal",dataList.getIssueTotal());
                                       m.put("preissueMembership",dataList.getIssueMembership());
                                   }else{
                                       m.put("preissueTotal",0);
                                       m.put("preissueMembership",0);
                                   }
                               }
                           }
                       }
                   }
               }
               list.add(m);
           }
       }else if(cardAnalyzeInDTO.getQueryType()==2){//处理门店的数据
           for(CardAnalyzeOutDTO cdList:issueList){
               Map m = new HashMap();
               m.put("industryID",cdList.getIndustryID());
               m.put("industryName",cdList.getIndustryName());
               m.put("merchantName",cdList.getMerchantName());
               m.put("cardKxTotal",0);
               m.put("kxMoneyTotal",0);
               m.put("issueTotal",cdList.getIssueTotal());
               m.put("issueMembership", cdList.getIssueMembership());

               m.put("precardKxTotal",0);
               m.put("prekxMoneyTotal",0);
               m.put("preissueTotal",0);
               m.put("preissueMembership",0);

               m.put("cardKxTotalContrast",0);
               m.put("kxMoneyTotalContrast",0);
               m.put("issueTotalContrast",0);
               m.put("issueMembershipContrast",0);

               m.put("total",0); //本月总数
               m.put("totalContrast", 0);

               m.put("industryIssuetotal",cdList.getIndustryIssuetotal());      //卡券发放总数
               m.put("industryIssuetotalContrast",0);//每个行业卡券发放总数占比

               if(preissueList!=null||preissueList.size()>0) {  //上月
                   for(CardAnalyzeOutDTO dataList:preissueList){
                       if(cdList.getMerchantName().equals(dataList.getMerchantName())){
                           m.put("preissueTotal",dataList.getIssueTotal());
                           m.put("preissueMembership",dataList.getIssueMembership());

                       }
                   }
               }

                   for (CardAnalyzeOutDTO kxlist : kxList) {  // 卡券核销  //本月的
                       if (cdList.getMerchantName().equals(kxlist.getMerchantName())) {
                           m.put("cardKxTotal", kxlist.getCardKxTotal());
                           m.put("kxMoneyTotal", kxlist.getKxMoneyTotal());
                           m.put("total",kxlist.getTotal());
                           if (prekxList != null || prekxList.size() > 0) {  //上月的
                               for (CardAnalyzeOutDTO preList : prekxList) {
                                   if (preList.getMerchantName().equals(cdList.getMerchantName())) {
                                       m.put("precardKxTotal", preList.getCardKxTotal());
                                       m.put("prekxMoneyTotal", preList.getKxMoneyTotal());
                                   }
                               }
                           }
                       }
                   }

               list.add(m);
           }
       }



        for(int i=0;i<list.size();i++){
            Map m=(HashMap)list.get(i);
            //卡券核销总数
            int cardKxTotal = Integer.parseInt(m.get("cardKxTotal").toString());
            int precardKxTotal = Integer.parseInt(m.get("precardKxTotal").toString());
            if(cardKxTotal==0&&precardKxTotal!=0){
                m.put("cardKxTotalContrast", "-" + 100);
            }else if(cardKxTotal!=0&&precardKxTotal==0){
                m.put("cardKxTotalContrast", "+" + 100);
            }else if(cardKxTotal!=0&&precardKxTotal!=0){
                float cardKxTotalContrast = ((float)(cardKxTotal - precardKxTotal) / precardKxTotal)*100;
                if(cardKxTotal>precardKxTotal){
                    m.put("cardKxTotalContrast","+"+(float) Math.round(cardKxTotalContrast*100)/100);
                }else{
                    m.put("cardKxTotalContrast",(float) Math.round(cardKxTotalContrast*100)/100);
                }
            }
            //核销金额总计
            float kxMoneyTotal = Float.parseFloat(m.get("kxMoneyTotal").toString());
            float prekxMoneyTotal = Float.parseFloat(m.get("prekxMoneyTotal").toString());
            if(kxMoneyTotal==0&&prekxMoneyTotal!=0){
                m.put("kxMoneyTotalContrast", "-" + 100);
            }else if(kxMoneyTotal!=0&&prekxMoneyTotal==0){
                m.put("kxMoneyTotalContrast", "+" + 100);
            }else if(kxMoneyTotal!=0&&prekxMoneyTotal!=0){
                float kxMoneyTotalContrast = ((kxMoneyTotal - prekxMoneyTotal) / prekxMoneyTotal)*100;
                if(kxMoneyTotal>prekxMoneyTotal){
                    m.put("kxMoneyTotalContrast","+"+(float) Math.round(kxMoneyTotalContrast*100)/100);
                }else{
                    m.put("kxMoneyTotalContrast",(float) Math.round(kxMoneyTotalContrast*100)/100);
                }
            }

            //卡券发放总数
            int issueTotal = Integer.parseInt(m.get("issueTotal").toString());
            int preissueTotal = Integer.parseInt(m.get("preissueTotal").toString());
            if(issueTotal==0&&preissueTotal!=0){
                m.put("issueTotalContrast", "-" + 100);
            }else if(issueTotal!=0&&preissueTotal==0){
                m.put("issueTotalContrast", "+" + 100);
            }else if(issueTotal!=0&&preissueTotal!=0){
                float issueTotalContrast = ((float) (issueTotal - preissueTotal) / (float) preissueTotal)*100;
                if(issueTotal>preissueTotal){
                    m.put("issueTotalContrast","+"+(float) Math.round(issueTotalContrast*100)/100);
                }else{
                    m.put("issueTotalContrast",(float) Math.round(issueTotalContrast*100)/100);
                }
            }
            //发放会员数
            int issueMembership = Integer.parseInt(m.get("issueMembership").toString());
            int preissueMembership = Integer.parseInt(m.get("preissueMembership").toString());
            if(issueMembership==0&&preissueMembership!=0){
                m.put("issueMembershipContrast", "-" + 100);
            }else if(issueMembership!=0&&preissueMembership==0){
                m.put("issueMembershipContrast", "+" + 100);
            }else if(issueMembership!=0&&preissueMembership!=0){
                float issueMembershipContrast = ((float) (issueMembership - preissueMembership) / (float) preissueMembership)*100;
                if(issueMembership>preissueMembership){
                    m.put("issueMembershipContrast","+"+(float) Math.round(issueMembershipContrast*100)/100);
                }else{
                    m.put("issueMembershipContrast",(float) Math.round(issueMembershipContrast*100)/100);
                }
            }

            //占比
            int total = Integer.parseInt(m.get("total").toString());
            int pretotal = Integer.parseInt(m.get("cardKxTotal").toString());
            float totalContrast = ((float) pretotal / (float) total)*100;
            m.put("totalContrast",(float) Math.round(totalContrast*100)/100);

            int industryIssuetotal = Integer.parseInt(m.get("industryIssuetotal").toString());
            int curissueTotal = Integer.parseInt(m.get("issueTotal").toString());
            float industryIssuetotalContrast = ((float) curissueTotal / (float) industryIssuetotal)*100;
            m.put("industryIssuetotalContrast",(float) Math.round(industryIssuetotalContrast*100)/100);
        }
        cardDataOutDTO.setKxList(list);
        return cardDataOutDTO;
    }

    @Override
    public CardDataOutDTO queryCouponIssue(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception {
        CardDataOutDTO cardDataOutDTO = new CardDataOutDTO();
        List dataList = new ArrayList();
        List<CardAnalyzeOutDTO> list = tradeOfflineMapper.queryCouponIssue(cardAnalyzeInDTO);
        List<CardAnalyzeOutDTO> kxList = tradeOfflineMapper.queryCouponKx(cardAnalyzeInDTO);
        for(CardAnalyzeOutDTO issueList:list){
            Map m = new HashMap();
            m.put("couponName",issueList.getCouponName());
            m.put("couponType", issueList.getCouponType());
            m.put("total", issueList.getTotal());
            m.put("issueTotal", issueList.getIssueTotal());
            m.put("issueMembership", issueList.getIssueMembership());
            m.put("couponValue", issueList.getCouponValue());
            m.put("cardKxTotal", 0);
            m.put("kxMembership", 0);
            m.put("kxtotal", 0);
            m.put("totalContrast", 0); //核销率
            for(CardAnalyzeOutDTO kxlist:kxList){
                if(issueList.getCouponID()==kxlist.getCouponID()){
                    m.put("cardKxTotal", kxlist.getCardKxTotal());
                    m.put("kxMembership", kxlist.getKxMembership());
                    m.put("kxtotal",kxlist.getKxtotal());
                    Integer cardKxTotal = kxlist.getCardKxTotal();
                    Integer kxtotal = kxlist.getKxtotal();
                   float totalContrast= ((float)cardKxTotal/(float)kxtotal)*100;
                    m.put("totalContrast", Math.round(totalContrast*100)/100); //核销率
                }
            }
            dataList.add(m);
        }
        cardDataOutDTO.setKxList(dataList);
        return cardDataOutDTO;
    }

    @Override
    public CardDataOutDTO queryCouponKxMoneyGroupBy(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception {
        CardDataOutDTO cardDataOutDTO = new CardDataOutDTO();
        List<CardAnalyzeOutDTO> kxList=null;
        System.out.println();
        if(1==cardAnalyzeInDTO.getQueryType()){
            kxList = tradeOfflineMapper.queryCouponKxMoneyGroupBy(cardAnalyzeInDTO);
        }
       else if(2==cardAnalyzeInDTO.getQueryType()){
            kxList = tradeOfflineMapper.queryCouponIssueMoneyGroupBy(cardAnalyzeInDTO);
        }

        List<Industry> industryList = industryMapper.findFirstLevel();

        List list = new ArrayList();
        List list3 = new ArrayList();
        for(Industry du:industryList){
            Map m = new HashMap();
            m.put("industryName", du.getName());
            String str = "";
            List listdu = new ArrayList();
            for(CardAnalyzeOutDTO cdList:kxList){
                Map map = new HashMap();
               if(!str.contains(cdList.getCouponValue2().toString())) {
                    str += cdList.getCouponValue2();
                   map.put("couponValue2", cdList.getCouponValue2());
                   //map.put("cardKxTotal", 0);
                   listdu.add(map);
              }
            }
            m.put("industryList", listdu);
            list.add(m);
        }
        String str = "";
        for(CardAnalyzeOutDTO cdList:kxList){
            for(int i=0;i<list.size();i++){
                Map m=(HashMap)list.get(i);
                List listdu=(ArrayList)m.get("industryList");
                if(cdList.getIndustryName().equals(m.get("industryName").toString())){

                    for(int j=0;j<listdu.size();j++){
                        Map map=(HashMap)listdu.get(j);
                        if((map.get("couponValue2").toString()).equals(cdList.getCouponValue2().toString())){
                            map.put("cardKxTotal", cdList.getCardKxTotal());
                            if(!str.contains(map.get("couponValue2").toString())) {
                                str +=map.get("couponValue2").toString();
                                Map m3 = new HashMap();
                                String couponValue=map.get("couponValue2").toString();
                                //int indexOf = couponValue.indexOf(".");
                                m3.put("couponValue2",Integer.parseInt(couponValue)/100);
                                System.out.println(couponValue);
                                list3.add(m3);
                            }
                        }
                    }
                }
            }
        }
        System.out.println(list);
        cardDataOutDTO.setObjList(list);
        cardDataOutDTO.setIssueList(list3);
        System.out.println(list3);
        return cardDataOutDTO;
    }

    @Override
    public CardDataOutDTO queryTradeOffineExport(CardAnalyzeInDTO cardAnalyzeInDTO) throws Exception {
        CardDataOutDTO cardDataOutDTO = new CardDataOutDTO();
        List<CardAnalyzeExportOutDTO> list = tradeOfflineMapper.queryTradeOffineExport(cardAnalyzeInDTO);
        for(CardAnalyzeExportOutDTO cdList :list){
            Float orderAmount = cdList.getOrderAmount()/100;
            cdList.setOrderAmount((float)Math.round(orderAmount*100)/100);

            float discountAmount = cdList.getDiscountAmount() / 100;
            cdList.setDiscountAmount((float)Math.round(discountAmount*100)/100);

            Float realAmount = cdList.getRealAmount()/100;
            cdList.setRealAmount((float)Math.round(realAmount*100)/100);
        }
        cardDataOutDTO.setListCardAnalyzeExport(list);
        return cardDataOutDTO;
    }
}
